﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using E_Shop.Common;
using ESHOP.DAO;
using ESHOP.Domain;
using ESHOP.UTIL;

namespace E_Shop
{
    public partial class ShoppingDefault : Authentication
    {
        private readonly DanhMucDAO _danhmuc = new DanhMucDAO();
        private SanPhamDAO _sanphamDao = new SanPhamDAO();
        private GioHangDAO _giohangDao = new GioHangDAO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDataforRepeater();
            }
        }
        public void BindDataforRepeater()
        {

            var sanPhams = _sanphamDao.GetList();
            CollectionPager1.MaxPages = 10000;
            CollectionPager1.PageSize = 16; // số items hiển thị trên một trang
            CollectionPager1.DataSource = sanPhams;
            CollectionPager1.BindToControl = DtlSanpham;
            DtlSanpham.DataSource = CollectionPager1.DataSourcePaged;
            DtlSanpham.DataBind();
        }
        protected void DtlSanpham_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        protected void btnAddcard_Click(object sender, EventArgs e)
        {
            GIOHANG gioHang = new GIOHANG();
            var button = (LinkButton)sender;
            var idsp = button.CommandArgument.ToString();
            var giohangs = _giohangDao.GetListBuyCompanyId(Acount.MACT);
            int flag = 1;
            for (int i = 0; i < giohangs.Count; i++)
            {
                if (giohangs[i].MASP == idsp)
                {
                     _giohangDao.UpdateNumber(giohangs[i].ID,1);
                     flag = 0;
                     break;
                }
            }
            if (flag == 1)
            {
                SANPHAM sp = new SANPHAM();
                sp = _sanphamDao.Get(idsp);                
                gioHang.MACT = Acount.MACT;
                gioHang.MASP = idsp;
                gioHang.SOLUONG = 1;
                gioHang.GIA = sp.GIA; 
                gioHang.ISSHOW = true;
                _giohangDao.Insert(gioHang);
                
            }
            Session[Constants.SESSION_GIOHANG] = gioHang;
            Response.Redirect(Request.Url.ToString());

        }
    }
}